﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.IO;

namespace DynaSpace
{
	static class Program
	{
		const string PARFULLSCREEN = "fullscreen";
		
		/// <summary>
		/// Vstupní bod aplikace
		/// </summary>
		static void Main(string[] args)
		{
			// předal uživatel parametrem dll assembly, ktere se maji nacist?
			if (args.Length > 0)
			{
				// zjistit dll komponenty hry
				string componentFileName = args[0];

				// zjistit dalsi parametry
				var parameters = new HashSet<string>();
				for (int i = 1; i < args.Length; i++)
				{
					parameters.Add(args[i].ToLower());	
				}

				// fullscreen rezim?
				bool fullScreen = false;
				if (parameters.Contains(PARFULLSCREEN)) 
				{
					fullScreen = true;
				}

				RunGame(new string[] { componentFileName }, fullScreen);
				return;
			}

			// ziskat soubory pluginu
			var files = ComponentManager.GetAllFiles();
			
			// nechat vybrat uzivatelem pluginy
			using (var frm = new FormChoosePlugins(files))
			{
				while (true)
				{
					if (frm.ShowDialog() != DialogResult.OK)
						return; // konec

					// spustit hru
					RunGame(frm.GetSelectedFiles(), frm.Fullscreen);
				}
			}
		}

		private static void RunGame(string[] selectedFiles, bool fullScreen)
		{
			// spustit hru
			using (DynaGame game = new DynaGame())
			{
				//try
				//{
					var components = ComponentManager.LoadComponents(game, selectedFiles); // načíst všechny komponenty
					components.ForEach(component => game.Components.Add(component)); // přidat komponenty do hry
					game.graphics.IsFullScreen = fullScreen;
					game.Run(); // spustit hru
				//}
				//catch (Exception err)
				//{
				//	MessageBox.Show(err.Message + "\n\nStack:\n" + err.StackTrace, "Nastala neočekávaná chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
				//}
			}
		}
	}
}

